﻿import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import "../components"

Item {

    id: pageMaintain

    property int maintainPage: 0
    property string currentDevice: ""  // 当前维护的设备
    property int currentDeviceStatus: 0     // 当前维护设备的状态 0:未连接 1:在线 2:运行中 3:已完成 4:故障

    property string deviceName: "当前没有设备"

    onVisibleChanged: {
        init()
        // [TODO] 获取设备列表
        // 查看当前维护的设备是否还处于连接状态
        var equList = CSetUp.queryEquInfo()
        if(equList.length === 0){
            currentDevice = ""
            deviceName = "当前没有设备"
        }else{
            for(var key in equList){
                currentDevice = key
                console.log("CURRENT_DEVICE:", key)
                break
            }
            MACO.sbChange(currentDevice)
            currentDeviceStatus = equList[currentDevice]["EquStatus"]
            deviceName = equList[currentDevice]["EquName"] + "   " +
                    (equList[currentDevice]["EquConnectType"]===0 ? equList[currentDevice]["Ip"] : equList[currentDevice]["SerialPort"]) + ":" +
                    (equList[currentDevice]["EquConnectType"]===0 ? equList[currentDevice]["Port"] : equList[currentDevice]["BaudRate"])
        }
        boxDeviceList.selectedDevice = 0
        pswd.visible = true
    }

    function init(){
        // 初始化按钮样式
        var list = layout_tab.children
        for(let i = 0; i < list.length; i ++){
            list[i].button_color = "transparent"
            list[i].text_color = "#666666"
            list[i].button_color_select = "white"
            list[i].text_color_select = "#1677FF"
        }
    }

    Connections{
        target: mainwindow
        onTabMaintain:{
        }
        onAskForTab:{
            if(mainwindow.mainPage !== 5) return
            mainwindow.permitTab()
        }
    }

    BoxDeviceList{
        id: boxDeviceList
        onAccept: {
            currentDevice = equId
            var equList = CSetUp.queryEquInfo()
            MACO.sbChange(equId)
            currentDeviceStatus = equList[equId]["EquStatus"]
            deviceName = equList[equId]["EquName"] + "   " +
                    (equList[equId]["EquConnectType"]===0 ? equList[equId]["Ip"] : equList[equId]["SerialPort"]) + ":" +
                    (equList[equId]["EquConnectType"]===0 ? equList[equId]["Port"] : equList[equId]["BaudRate"])
        }onReject: {

        }
    }
    BoxOtherMaintain{
        id: boxOtherMaintain
        onAccept:{

        }onReject:{

        }
    }

    // 样式 =======================================================================

    anchors.fill: parent
    anchors.margins: 15*dp

    Rectangle{                      // 左侧的页面
        y: 60*dp
        width: parent.width-300*dp
        height: parent.height-60*dp
        clip: true
        Rectangle{
            color: "#EFEFF2"
            height: 60*dp
            width: parent.width
            Row{
                id: layout_tab
                anchors.fill: parent
                PushButton{
                    height: 60*dp
                    width: 120*dp
                    title: "热盖"
                    isSelected: maintainPage === 0
                    onReleased: {
                        maintainPage = 0
                    }
                }PushButton{
                    height: 60*dp
                    width: 120*dp
                    title: "荧光检测"
                    isSelected: maintainPage === 1
                    onReleased: {
                        maintainPage = 1
                    }
                }PushButton{
                    height: 60*dp
                    width: 120*dp
                    title: "加热块"
                    isSelected: maintainPage === 2
                    onReleased: {
                        maintainPage = 2
                    }
                }
            }
        }
        Item{
            y: 70*dp
            height: parent.height - 60*dp
            width: parent.width
            MaintainHeatLid{
                visible: maintainPage === 0
            }
            MaintainFluorescence{
                visible: maintainPage === 1
            }
            MaintainHeatBlock{
                visible: maintainPage === 2
            }
        }
        visible: currentDevice !== ""
    }

    Item{                               // 右侧的图标
        y: 50*dp
        x: parent.width - 300*dp
        width: 300*dp
        height: parent.height-50*dp
        Image{
            id: imageDevice
            height: 242*dp
            width: 269*dp
            x: 20*dp
            anchors.verticalCenter: parent.verticalCenter
            anchors.verticalCenterOffset: -100*dp
            source: maintainPage===0 ? "qrc:/picture/machine0.png" : (maintainPage===1 ? "qrc:/picture/machine1.png" : "qrc:/picture/machine2.png")
        }
        visible: currentDevice !== ""
    }

    Rectangle{                  // 设备为空 / 设备未连接 / 设备运行中
        y: 60*dp
        width: parent.width-300*dp
        height: parent.height-60*dp
        visible: currentDevice === "" || currentDeviceStatus === 0 || currentDeviceStatus === 2
        Image{
            width: 300*dp
            height: 280*dp
            anchors.centerIn: parent
            anchors.verticalCenterOffset: -100*dp
            source: {
                if(currentDevice === "")                 return "qrc:/picture/empty.png"
                else if(currentDeviceStatus === 0)       return "qrc:/picture/offline.png"
                else if(currentDeviceStatus === 2)       return "qrc:/picture/free.png"
                else                                     return "qrc:/picture/empty.png"
            }
            Text{
                text: {
                    if(currentDevice === "")             return "您还没有设备，请先添加设备！"
                    else if(currentDeviceStatus === 0)   return "当前设备未连接！"
                    else if(currentDeviceStatus === 2)   return "无法维护运行中设备"
                    else                                 return ""
                }
                font.pixelSize: config.fontsize + 2
                anchors.horizontalCenter: parent.horizontalCenter
                y: 280*dp
                color: "#AAAAAA"
            }
            PushButton{
                title: "设备管理"
                visible: currentDevice === ""
                defaultMode2: true
                anchors.horizontalCenter: parent.horizontalCenter
                y: 340*dp
                onReleased: {
                    mainwindow.mainPage = 6
                    mainwindow.tabSettings()
                }
            }
        }
    }

    Item{                           // 上面的条
        id: layout_top
        height: 40 *dp
        width: parent.width
        Text{
            y: -5*dp
            text: "维护"
            font.pixelSize: config.fontsize+4
            font.bold: true
            anchors.verticalCenter: parent.verticalCenter
        }
        Text{
            y: 35*dp
            font.pixelSize: config.fontsize-2
            color: "#888888"
            text: deviceName
        }
        PushButton{
            button_icon: "qrc:/picture/changeDevice.png"
            title: "切换设备"
            radius: 15*dp
            height: 30*dp
            width: 105*dp
            x: 50*dp
            y: 5*dp
            button_color: "#E5E5FF"
            onReleased: {
                boxDeviceList.show()
            }

        }

        PushButton{
            title: "其它维护"
            defaultMode2: true
            y: 5*dp
            x: parent.width-380*dp - 301*dp
            onReleased: {
                boxOtherMaintain.show()
            }
            isDisabled: currentDevice === ""
        }
        PushButton{
            title: "设备自检"
            defaultMode2: true
            y: 5*dp
            x: parent.width-250*dp - 301*dp
            onReleased: {
                MACO.sbzj()
            }
            isDisabled: currentDevice === ""
        }
        CustomComboBox{
            model: ["操作","主控保存","主控复位"]
            y: 5*dp
            textWidth: 40*dp
            editWidth: 120*dp
            x: parent.width-120*dp - 301*dp-40*dp
            onCurrentText: {
                console.log("维护点击事件触发")
                MACO.zkcz(text);
            }
        }

        Rectangle{
            id: operateLayout
            x: parent.width-140*dp - 300*dp
            y: 60*dp
            height: 150*dp
            width: 140*dp
            Column{
                x: 10*dp
                spacing: 10*dp
                PushButton{
                    defaultMode2: true
                    title: "保存"
                }PushButton{
                    defaultMode2: true
                    title: "复位"
                }PushButton{
                    defaultMode2: true
                    title: "恢复初始"
                }
            }
            onVisibleChanged: {
                if(!visible){
                    visible = false
                }
            }
        }
    }

    CustomPassword{
        id: pswd
        pstype: 1
    }

}
